package com.hpin.fansea.home;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.alipay.AlipayEx;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.PropOrdeBean;
import com.hpin.fansea.bean.PropertyFeeOrderVO;
import com.hpin.fansea.bean.PropertyResBean;
import com.hpin.fansea.utils.CommonUtils;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.hpin.fansea.utils.LogUtil;
import com.hpin.fansea.wxapi.WXPayEx;
import com.lidroid.xutils.http.RequestParams;

/**
 * 历史欠费缴费
 * Created by Aaron on 15/10/26.
 */
public class PayHistoryActivity extends BaseActivity {

    private ImageView iv_weixin;
//    private ImageView iv_zhifubao;
//    private ImageView iv_one;
//    private ImageView iv_three;
//    private ImageView iv_six;
//    private ImageView iv_year;
    private EditText et_pay;
//    private TextView tv_this_year;
    private TextView tv_history;
    private String contact;
    private String phone;
    private String unitName;
    private String invoiceType; //发票类型 0是个人 1 是单位 空 是不需要发票
    private PropertyResBean.PropertyData propertyData;
    private double monthFee;//每月物业费

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_history_pay);

        //微信支付初始化
        wxpe.initWXAPI(this);

        contact = getIntent().getStringExtra("contact");
        phone = getIntent().getStringExtra("phone");
        unitName = getIntent().getStringExtra("unitName");
        invoiceType = getIntent().getStringExtra("invoiceType");

        propertyData = (PropertyResBean.PropertyData) getIntent().getSerializableExtra("propertyData");

        initWidget();

        if (propertyData != null) {

//            tv_this_year.setText(propertyData.propertyFee + "");
            tv_history.setText(propertyData.historyFee + "");
            monthFee = propertyData.monthFee;

        }

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.pay));

        iv_left.setOnClickListener(this);

//        tv_this_year = (TextView) findViewById(R.id.tv_pay_this_year);
        tv_history = (TextView) findViewById(R.id.tv_history);

        et_pay = (EditText) findViewById(R.id.et_pay);


//        iv_one = (ImageView) findViewById(R.id.iv_one);
//        iv_three = (ImageView) findViewById(R.id.iv_three);
//        iv_six = (ImageView) findViewById(R.id.iv_six);
//        iv_year = (ImageView) findViewById(R.id.iv_year);

        RelativeLayout rl_weix = (RelativeLayout) findViewById(R.id.rl_weix);
//        RelativeLayout rl_zhifb = (RelativeLayout) findViewById(R.id.rl_zhifb);

        iv_weixin = (ImageView) findViewById(R.id.iv_weixin);
//        iv_zhifubao = (ImageView) findViewById(R.id.iv_zhifubao);

        Button btn_commit = (Button) findViewById(R.id.btn_commit);

        rl_weix.setOnClickListener(this);
//        rl_zhifb.setOnClickListener(this);
        btn_commit.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;

            case R.id.rl_weix:

                iv_weixin.setSelected(true);
//                iv_zhifubao.setSelected(false);

                break;

//            case R.id.rl_zhifb:
//
//                iv_weixin.setSelected(false);
//                iv_zhifubao.setSelected(true);
//
//                break;

            case R.id.btn_commit:

                commit();

                break;

        }

    }

    /**
     * 提交
     */
    private void commit() {

        if (CommonUtils.isNull(et_pay.getText().toString().trim())) {
            showToast("请输入缴费金额");
            return;
        }

//        if (!iv_weixin.isSelected() && !iv_zhifubao.isSelected()) {
//            showToast("请选择支付方式");
//            return;
//        }
        if (!iv_weixin.isSelected()) {
            showToast("请选择支付方式");
            return;
        }

        PropertyFeeOrderVO fee = new PropertyFeeOrderVO();
        fee.systemType = Constant.SYSTEM_TYPE;
        fee.token = sp.getString(Constant.token, "");
        if (propertyData != null)
            fee.propertyFeeId = propertyData.propertyFeeId;// 物业费记录ID
        fee.memId = phone;//用户id  手机号
        fee.memName = contact;//用户名 联系人
        fee.mobNo = phone;//用户联系方式
        fee.invoiceType = invoiceType;//发票类型
        fee.invoiceTitle = unitName;//发票抬头
        fee.payType = iv_weixin.isSelected() ? Constant.PAYMETHOD_WEIXIN : Constant.PAYMETHOD_ZHIFUBAO;
        fee.ordCash = et_pay.getText().toString().trim();
//        fee.ordCash = "0.1";

        RequestParams params = new RequestParams();
        params.addBodyParameter("propertyFeeOrder", JSONObject.toJSONString(fee));

        LogUtil.e("tag", "支付请求的数据===" + JSONObject.toJSONString(fee));

        HttpUtil.getInstance().setPostRequest("正在下单", this, Constant.WEB_ROOT + Constant.PROPERTYORDER, params, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    PropOrdeBean propOrdeBean = JSONObject.parseObject(str, PropOrdeBean.class);

                    if (Constant.STATUS_SUCESS.equals(propOrdeBean.status)) {

                        if (propOrdeBean == null)
                            return;

                        //订单生成成功, 启动支付
                        payOrder(propOrdeBean);

                    } else {
                        showToast(propOrdeBean.message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showToast("下单失败");
                }

            }
        }, null);

    }

    // 微信支付
    private WXPayEx wxpe = new WXPayEx();
    /**
     * 启动支付
     *
     * @param
     */
    private void payOrder(PropOrdeBean propOrdeBean) {

        if (propOrdeBean.result == null)
            return;

        String payType = iv_weixin.isSelected() ? Constant.PAYMETHOD_WEIXIN : Constant.PAYMETHOD_ZHIFUBAO;

        if (payType.equals(Constant.PAYMETHOD_WEIXIN)) {// 微信支付

            wxpe.doStartWXPay(propOrdeBean.result.prepayid, propOrdeBean.result.nonce_str,
                    propOrdeBean.result.timestamp, propOrdeBean.result.package_, propOrdeBean.result.sign);

            //物业支付
            mApplication.wxPayType = Constant.PAY_PROPERTY;

        } else
        if (payType.equals(Constant.PAYMETHOD_ZHIFUBAO)) {// 支付宝支付

            AlipayEx.doStartAlipay(Constant.PAY_PROPERTY, this, propOrdeBean.result.out_trade_no,
                    propOrdeBean.result.subject, propOrdeBean.result.body, propOrdeBean.result.total_fee,
                    propOrdeBean.result.notify_url, propOrdeBean.result.return_url);
        }
    }

}
